// 在页面中定义插屏广告
let interstitialAd = null

var util = require('../../utils/util.js')
const app = getApp()
let keyWord = app.globalData.pingtai_tag
let count = 10
let resCount = null
Page({
  data: {
    // 访问量
    fangwencishu: 0,
    fangwenrenshu: 0,
    // 页面显示
    sendNews: false,
    // 组件
    TabCur: 0,
    scrollLeft: 0,
    newsList: [],
    daodile: false
  },
    // 确定发布消息
    fatie() {
      wx.navigateToMiniProgram({
        appId: 'wx83ed21db6ad34120',
        path: `/pages/news/news`,
        extraData: {
          pingtai: keyWord
        },
        envVersion: 'release',
        success(res) {
          // 打开成功
        }
      })
    },
  // 在适合的场景显示插屏广告
  interstitialAdShow() {
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {console.log(err,'插屏广告报错');})
    }
  },
  // 在页面onLoad回调事件中创建插屏广告实例 
  createInterstitialAd() {
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-90fd639d91760093'
      })
    }
  },
  onShow() {
    this.tabBar()
    this.getFangwen() // 查询访问量
    this.interstitialAdShow() //显示插屏广告
  },
  onLoad: function () {
    if(!app.globalData.isRegistered){
      app.selecteUser().then(res=>{
        app.globalData.isRegistered = res
      })
    }
    this.createInterstitialAd() //创建插屏广告实例
    this.getSystemController() //获取系统控制变量
    this._loadNewsList(0) // 加载帖子列表
    this.getShareImg() // 获取分享图片
  },
  //异步获取系统控制变量
  getSystemController() {
    if (app.globalData.controller == null) {
      app.getSystemController().then(res => {
        this.setData({
          sendNews: res.sendNews
        })
      })
    } else {
      this.setData({
        sendNews: app.globalData.controller.sendNews
      })
    }
  },

  // 加载帖子列表
  _loadNewsList(start = 0, tag = 'selectByClassName') {
    wx.showLoading({
      title: 'Loading...',
    })
    let baseUrl = 'https://zjb.fendou.life/newslist/list?'
    let url = `${baseUrl}infoTag=${keyWord}&start=${start}&count=${count}`
    wx.request({
      url,
      success: res => {
        this.setData({
          newsList: this.data.newsList.concat(res.data)
        })
        wx.stopPullDownRefresh()
        wx.hideLoading()
        resCount = res.data.length
      },
      fail: res => {}
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getFangwen()
    this.setData({
      newsList: [],
      TabCur: 0,
      scrollLeft: 0
    })
    this._loadNewsList(0)
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  handscrolltolower() {
    this.setData({
      daodile: true
    })
    this.onReachBottom()
  },
  onReachBottom() {
    if (resCount < count) {
      wx.showToast({
        icon: 'none',
        title: '没有数据了',
      })
    } else {
      this._loadNewsList(this.data.newsList.length)
    }
  },
  // 放大显示图片
  onPreviewImage(e) {
    wx.previewImage({
      urls: e.target.dataset.imgs,
      current: e.target.dataset.imgs[e.target.dataset.id]
    })
  },
  // 查询访问量
  getFangwen() {
    wx.cloud.callFunction({
      name: 'fangwen',
      data: {
        tag: 'selectFangwen',
        _updateTime: util.formatTime(new Date()).slice(0, 10)
      }
    }).then(res => {
      this.setData({
        fangwencishu: res.result.avatar,
        fangwenrenshu: res.result.renshu,
      })
    })
  },
  // 获取分享图片及文案
  getShareImg() {
    app.getGlobalShareMedia().then(res => {
      newsShareMinImg = res.newsShareMinImg
      newsShareText = res.newsShareText
    })
  },
 


  // 发帖和置顶操作
  sendNews(e) {

    wx.navigateTo({
      url: `/pages/send_news/send_news?tag=${e.currentTarget.dataset.tag}`,
    })


  },
  // 组件
  isCard(e) {
    this.setData({
      isCard: e.detail.value
    })
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },

  tabBar() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    let title = `${app.globalData.userInfo.nickName}带你上高速`
    let imageUrl = app.globalData.userInfo.avatarUrl
    if(e.from == 'button'){
       title = e.target.dataset.news.content
       imageUrl = e.target.dataset.news.img[0]
    }
    return {
      title,
      imageUrl,
      path: `/pages/news/news?shangjiOpenId=${app.globalData.userInfo._openid}`,
    }
  },
})